package fastjsonDemo.JSONObjectDemo;

import com.alibaba.fastjson.JSONObject;
import org.junit.Test;

import java.util.Date;

/**
 * JSONObject 相当于一个Map
 */
public class demo1 {
	static JSONObject jsonObject;

	static {
		pojo1 pojo1 = new pojo1(1, "张三");

		jsonObject = new JSONObject();
		jsonObject.put("key1", "AAA");
		jsonObject.put("key2", 11);
		jsonObject.put("key3", new Date());
		jsonObject.put("key4", pojo1);
	}

	@Test
	public void ceui1() {


		//转json ,下面两个API的效果是一样的
		String s = jsonObject.toString();
		String jsonString = jsonObject.toJSONString();
		// 输出:  {"key1":"AAA","key2":11,"key3":1567144999465,"key4":{"id":1,"name":"张三"}}
		Object key1 = jsonObject.get("key1"); //输出 AAA

		boolean key11 = jsonObject.containsKey("key1"); //是否有指定的可以 true
		boolean key111 = jsonObject.containsKey("key111");//  false

		System.out.println("key1 = " + key1);

	}

	/**
	 * 内部类
	 */
	public static class pojo1 {

		private int id;
		private String name;
		//*********

		public pojo1(int id, String name) {
			this.id = id;
			this.name = name;
		}

		public int getId() {
			return id;
		}

		public void setId(int id) {
			this.id = id;
		}

		public String getName() {
			return name;
		}

		public void setName(String name) {
			this.name = name;
		}
	}


	@Test
	public void c_将json字符串转成JSONObject对象() {
		String jsonStr = "{\n" +
				"  \"flag\": true,\n" +
				"  \"code\": 200,\n" +
				"  \"message\": \"请求成功\",\n" +
				"  \"data\": \"\\n\\n\\n<?xml version = \\\"1.0\\\" encoding=\\\"UTF-8\\\" ?>\\n<DOCUMENT>\\n\\t<RETURN_CODE>000000</RETURN_CODE>\\n    <RETURN_MSG></RETURN_MSG>\\n\\t<CURPAGE>1</CURPAGE>\\n\\t<PAGECOUNT>1</PAGECOUNT>\\n\\t<TOTAL>1</TOTAL>\\n\\t<PAYAMOUNT>0.00</PAYAMOUNT>\\n\\t<REFUNDAMOUNT>0.00</REFUNDAMOUNT>\\n\\n\\t<QUERYORDER>\\n\\t\\t<MERCHANTID>105007473994127</MERCHANTID>\\n\\t\\t<BRANCHID>322000000</BRANCHID>\\n\\t\\t<POSID>041287924</POSID>\\n\\t\\t<ORDERID>12asdaasdasdsd11</ORDERID>\\n\\t\\t<ORDERDATE>20200106154709</ORDERDATE>\\n\\t\\t<ACCDATE>20200106</ACCDATE>\\n\\t\\t<AMOUNT>0.01</AMOUNT>\\n\\t\\t<STATUSCODE>1</STATUSCODE>\\n\\t\\t<STATUS>成功</STATUS>\\n\\t\\t\\n\\t\\t<REFUND>0.00</REFUND>\\n\\t\\t<SIGN>02656f4da254987ea16a7718221d331232ce35f8b582871a68153a93420cdbd44ced84e74c41fb5f0875cc2250f6f9bfa65db3a636f030afe9d4076b51bda74d5fb76ebb14bb0db51cd155f76abe7c161e70d5c07fd058c658d4db05c192dfdc2cd5ca6942485cad13c0f773535c1412ee00e95b99c2be401e42807e48170607</SIGN>\\n\\t</QUERYORDER>\\n\\n</DOCUMENT>\\n\\n\\n\\n\\n\\n\\r\\n\"\n" +
				"}";
		JSONObject jsonObject1 = JSONObject.parseObject(jsonStr); //将json字符串转成JSONObject对象
		Object code = jsonObject1.get("code"); //获取值
		Object data = jsonObject1.get("data");//获取值
		System.out.println("code = " + code);
		System.out.println("data = " + data);
	}

}
